﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李刚 
    //
    // 文件名：UserListOfArea.cs
    // 文件功能描述：主用户管理:地区中心机构只能管理该地区的医疗机构主用户
    // 创建标识：李刚2009-06-18
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;

using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Collections.Generic;
using System.IO;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class UserListOfArea : BasePage
    {
        IBLL_User iu = Factory.BLL_User();
        public string roleId;
        public UserListOfArea()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.主用户管理.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //StoreQureyCondition();
                this.BindUserByOthers();
            }
        }

        /// <summary>
        /// 根据条件读取用户
        /// </summary>
        private void BindUserByOthers()
        {
            HZ.DrugTrade.Model.Query.HospitalUsers hpUser = GetQureyCondition();
            if (hpUser == null)
            {
                StoreQureyCondition();
                hpUser = GetQureyCondition();
            }
            IList<HZ.DrugTrade.Model.View.HospitalUsers> lhu = new List<HZ.DrugTrade.Model.View.HospitalUsers>();
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;

            int TotalCount = 0;

            //验证方法是否能被正常调用
            try
            {
                lhu = iu.GetHospitalUsersByWhere(hpUser, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out TotalCount);
                AspNetPager1.RecordCount = TotalCount;
                gvwUser.PageSize = AspNetPager1.PageSize;
                this.gvwUser.DataSource = lhu;
                this.gvwUser.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "UserListOfArea.aspx");
            }
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.gvwUser.EditIndex = -1;
            this.StoreQureyCondition();
            //重置首页
            this.AspNetPager1.CurrentPageIndex = 1;
            //绑定用户列表
            BindUserByOthers();

            //string url = HttpContext.Current.Request.Path;
            //Response.Redirect(HttpContext.Current.Request.Path+"?"+this.AspNetPager1.UrlPageIndexName+"=1&" + Constants.QUERYSTRING_QUERY_CONDITION + "=" + this.QueryCondition);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            this.gvwUser.EditIndex = -1;
            BindUserByOthers();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUserRegister_Click(object sender, EventArgs e)
        {
            HZ.Web.WebUtil.RedirectByScript("UserRegister.aspx");
        }



        /// <summary>
        /// 获取查询条件存入ViewState
        /// </summary>
        /// <returns></returns>
        private HZ.DrugTrade.Model.Query.HospitalUsers StoreQureyCondition()
        {
            HZ.DrugTrade.Model.Query.HospitalUsers user = new HZ.DrugTrade.Model.Query.HospitalUsers();
            user.UserID = this.txtUserId.Text.Trim().ToUpper();
            user.UserName = this.txtUserName.Text.Trim();
            user.IsAdmin = true;
            //当然机构的所在地区
            user.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;
            if (this.ddlIsUsing.SelectedItem.Value.ToString() == "0")
            {
                user.IsUsing = false;
            }
            else if (this.ddlIsUsing.SelectedItem.Value.ToString() == "1")
            {
                user.IsUsing = true;
            }
            else
            {
                user.IsUsing = null;
            }

            if (this.ddlIsOnline.SelectedItem.Value.ToString() == "0")
            {
                user.IsOnline = false;
            }
            else if (this.ddlIsOnline.SelectedItem.Value.ToString() == "1")
            {
                user.IsOnline = true;
            }
            else
            {
                user.IsOnline = null;
            }
            if (dllState.SelectedIndex != -1 && dllState.SelectedIndex != 0)
            {
                user.RoleID = dllState.SelectedValue.ToString();
            }
            if (HZ.Common.MyValidate.IsDate(txtLoginFrom.Text.Trim()))
            {
                user.LastlogintimeFrom = Convert.ToDateTime(txtLoginFrom.Text.Trim());
            }
            if (HZ.Common.MyValidate.IsDate(txtLoginTo.Text.Trim()))
            {
                user.LastlogintimeTo = Convert.ToDateTime(txtLoginTo.Text.Trim());
            }
            user.SortFields = "Userid ASC";
            //存储到viewstate
            ViewState[Constants.VIEWSTATE_QUERY_CONDITION] = user;
            //存储到querystring
            //this.SetQueryCondition(user);

            return user;
        }

        /// <summary>
        /// 从ViewState中获取查询条件
        /// </summary>
        private HZ.DrugTrade.Model.Query.HospitalUsers GetQureyCondition()
        {
            HZ.DrugTrade.Model.Query.HospitalUsers user;

            user = (HZ.DrugTrade.Model.Query.HospitalUsers)ViewState[Constants.VIEWSTATE_QUERY_CONDITION];

            return user;
        }


        /// <summary>
        /// 执行锁定和解锁操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "IsUsing")
            {
                string userId = e.CommandArgument.ToString();
                List<string> userIds = new List<string>();
                userIds.Add(userId);
                //批量锁定
                if (iu.LockUser(userIds))
                {
                    //重新加载列表
                    this.BindUserByOthers();
                }
                else
                {
                    PageError("锁定/解锁用户失败！", "");
                }
            }
        }

        protected void btnBatLock_Click(object sender, EventArgs e)
        {
            string strChkUsers = Request["chkUser"];
            if (!string.IsNullOrEmpty(strChkUsers))
            {
                string[] arryUsers = strChkUsers.Split(new char[',']);
                List<string> userIds = new List<string>();
                for (int i = 0; i < arryUsers.Length; i++)
                {
                    userIds.Add(arryUsers[i]);
                }

                //批量锁定
                if (iu.LockUser(userIds))
                {
                    BindUserByOthers();
                    //PageInfo("成功锁定选定用户！", "");
                }
                else
                {
                    PageError("锁定用户失败！", "");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBatUnLock_Click(object sender, EventArgs e)
        {
            string strChkUsers = Request["chkUser"];
            if (!string.IsNullOrEmpty(strChkUsers))
            {
                string[] arryUsers = strChkUsers.Split(new char[',']);
                List<string> userIds = new List<string>();
                for (int i = 0; i < arryUsers.Length; i++)
                {
                    userIds.Add(arryUsers[i]);
                }

                //批量锁定
                if (iu.UnblockUser(userIds))
                {
                    BindUserByOthers();
                    //PageInfo("成功解锁选定用户！", "");
                }
                else
                {
                    PageError("解锁用户失败！", "");
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                int rowIndex = e.RowIndex;
                string userId = this.gvwUser.DataKeys[rowIndex].Value.ToString();
                string strUserName = ((TextBox)this.gvwUser.Rows[rowIndex].FindControl("txtUserName")).Text.Trim();
                if (string.IsNullOrEmpty(strUserName))
                {
                    HZ.Web.WebUtil.Alert("用户名称不能为空！", this);
                    ((TextBox)gvwUser.Rows[rowIndex].FindControl("txtUserName")).Focus();
                    return;
                }
                User user = new User();
                user.UserID = userId;
                user.UserName = strUserName;
                if (iu.UpdateUser(user) == 1)
                {
                    //重新绑定列表
                    this.gvwUser.EditIndex = -1;
                    this.BindUserByOthers();
                }
                else
                {
                    PageError("更新失败！", "");
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwUser.EditIndex = e.NewEditIndex;
            this.BindUserByOthers();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvwUser.EditIndex = -1;
            this.BindUserByOthers();
        }

        /// <summary>
        /// 初始化密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBatResetPws_Click(object sender, EventArgs e)
        {
            string strChkUsers = Request["chkUser"];
            if (!string.IsNullOrEmpty(strChkUsers))
            {
                string[] arryUsers = strChkUsers.Split(new char[',']);
                List<string> userIds = new List<string>();
                for (int i = 0; i < arryUsers.Length; i++)
                {
                    userIds.Add(arryUsers[i]);
                }

                if (iu.InitPassword(userIds, Constants.USER_DEFAULT_PWS))
                {
                    PageInfo("用户" + strChkUsers + "的密码已被初始化为" + Constants.USER_DEFAULT_PWS, "");
                }
                else
                {
                    PageError("密码初始化失败", "");
                }
            }

        }

        /// <summary>
        /// 切换 简单查询
        /// 高级查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (this.lbnSwitch.CommandArgument == "simple")
            {
                this.ltlAdvance.Text = "";
                this.lbnSwitch.CommandArgument = "advance";
                this.lbnSwitch.Text = "简单查询";
            }
            else
            {
                this.ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                this.lbnSwitch.CommandArgument = "simple";
                this.lbnSwitch.Text = "高级查询";

                //清空其他控件内容
                this.txtLoginFrom.Text = "";
                this.txtLoginTo.Text = "";
                this.txtUserName.Text = "";

            }

        }

        /// <summary>
        /// 批量设置用户权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBatSetOperate_Click(object sender, EventArgs e)
        {

            string strChkUsers = Request["chkUser"];
            //此处默认设置医疗机构
            string strRoleId = "01";
            string strRoleName = "医疗机构";

            if (!string.IsNullOrEmpty(strChkUsers) && !string.IsNullOrEmpty(strRoleId))
            {
                Response.Redirect("UserOperateBatch.aspx?uids=" + strChkUsers + "&rid=" + strRoleId + "&rname=" + HttpUtility.UrlEncode(strRoleName));
            }

        }
    }
}
